package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.dto.CommentWithGoodsDTO;
import com.example.demo.dto.CommentWithUserDTO;
import com.example.demo.entity.Comments;
import com.example.demo.entity.User;
import com.example.demo.mapper.CommentsMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 评论模块 服务实现类
 * </p>
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {
    @Autowired
    private CommentsMapper commentsMapper;

    @Autowired
    private UserMapper userMapper;

    // 添加评论并返回包含用户信息的评论DTO
    public CommentWithUserDTO addComment(Comments comment) {
        // 插入评论
        commentsMapper.insert(comment);

        // 获取刚插入的评论
        Comments savedComment = commentsMapper.selectById(comment.getCId());

        // 获取用户信息
        User user = userMapper.selectById(savedComment.getCUserId());

        // 创建DTO对象
        CommentWithUserDTO dto = new CommentWithUserDTO();
        dto.setCId(savedComment.getCId());
        dto.setCUserId(savedComment.getCUserId());
        dto.setCGoodsId(savedComment.getCGoodsId());
        dto.setCBio(savedComment.getCBio());
        dto.setUserName(user.getUName());

        return dto;
    }

    @Override
    public List<CommentWithUserDTO> getCommentsByGoods(Integer goodsId) {
        return commentsMapper.selectByGoodsIdWithUser(goodsId);
    }

    @Override
    public List<Comments> getCommentsByUser(Integer userId) {
        return commentsMapper.selectByUser(userId);
    }

    @Override
    @Transactional
    public String deleteComment(Integer commentId) {
        Comments comment = commentsMapper.selectById(commentId);
        if (comment == null) {
            return "评论不存在";
        }

        commentsMapper.deleteById(commentId);
        return "评论已删除";
    }

    @Override
    @Transactional
    public String updateComment(Comments comment) {
        Comments existingComment = commentsMapper.selectById(comment.getCId());
        if (existingComment == null) {
            return "评论不存在";
        }

        existingComment.setCBio(comment.getCBio());
        commentsMapper.updateById(existingComment);
        return "评论已更新";
    }

    @Override
    public IPage<CommentWithGoodsDTO> getCommentsWithGoodsByUser(Integer userId,
                                                                 Page<CommentWithGoodsDTO> page,
                                                                 String keyword) {
        return commentsMapper.selectCommentsWithGoodsByUser(page, userId, keyword);
    }
}
